package com.wm.ad_navi_new.base

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.wm.ad_navi_new.skin.ParseAttrUtil
import com.wm.ad_navi_new.skin.SkinItem
import com.wm.ad_navi_new.skin.SkinManager
import com.wm.ad_navi_new.skin.interfaces.ISkinUpdateListener

abstract class BaseFragment : Fragment(), ISkinUpdateListener{

    private var mSkinItemMap: Map<View, SkinItem>? = null

    override fun onAttach(context: Context) {
        super.onAttach(context)
        SkinManager.attach(this)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(setLayoutID(),container,false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mSkinItemMap = ParseAttrUtil.xmlLayoutParser(view,setLayoutID())
        initView(view)
        initData()
    }

    /**
     * 执行换肤
     */
    override fun onThemeUpdate() {
        mSkinItemMap?.let { ParseAttrUtil.updateSkin(it) }
    }

    // 设置布局ID
    abstract fun setLayoutID(): Int

    // 初始化View
    abstract fun initView(view: View)

    /**
     * 初始化数据
     */
    abstract fun initData()

}